package com.project2.service;

import com.project2.bean.bo.WxAddressSaveBo;
import com.project2.bean.po.MarketAddress;
import com.project2.bean.po.MarketAddressExample;
import com.project2.bean.vo.BeanInListInDataInWxAddressListVo;
import com.project2.bean.vo.DataInWxAddressDetailVo;
import com.project2.bean.vo.DataInWxAddressListVo;
import com.project2.mapper.MarketAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangminghui
 * @date 2022/8/15
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public DataInWxAddressListVo queryDataInWxAddressListVo(Integer currentUserId) {
        DataInWxAddressListVo dataInWxAddressListVo = new DataInWxAddressListVo();
        List<BeanInListInDataInWxAddressListVo> beanInListInDataInWxAddressListVos = marketAddressMapper.queryAllAddressInfo(currentUserId);
        Integer realAddressNum = 0;
        List<BeanInListInDataInWxAddressListVo> beanInListInDataInWxAddressListVos1 = new ArrayList<>();
        for (BeanInListInDataInWxAddressListVo beanInListInDataInWxAddressListVo : beanInListInDataInWxAddressListVos) {
            if (beanInListInDataInWxAddressListVo.getDeleted() == false) {
                realAddressNum++;
                beanInListInDataInWxAddressListVos1.add(beanInListInDataInWxAddressListVo);
            }
        }
        dataInWxAddressListVo.setTotal(realAddressNum);
        dataInWxAddressListVo.setPages(1);
        dataInWxAddressListVo.setLimit(realAddressNum);
        dataInWxAddressListVo.setPage(1);
        dataInWxAddressListVo.setList(beanInListInDataInWxAddressListVos1);
        return dataInWxAddressListVo;
    }

    @Override
    public DataInWxAddressDetailVo queryDataInWxAddressDetailVo(Integer id) {
        DataInWxAddressDetailVo dataInWxAddressDetailVo = marketAddressMapper.queryAddressInfoById(id);
        return dataInWxAddressDetailVo;
    }

    @Override
    public void saveAddressInfoByBo(WxAddressSaveBo wxAddressSaveBo, Integer currentUserId) {
        Date date = new Date();
        MarketAddress marketAddress = new MarketAddress();
        //marketAddress.setId(wxAddressSaveBo.getId());
        // todo: 根据数据表字段顺序确定插入字段顺序
        marketAddress.setName(wxAddressSaveBo.getName());
        marketAddress.setUserId(currentUserId);
        marketAddress.setProvince(wxAddressSaveBo.getProvince());
        marketAddress.setCity(wxAddressSaveBo.getCity());
        marketAddress.setCounty(wxAddressSaveBo.getCounty());
        marketAddress.setAddressDetail(wxAddressSaveBo.getAddressDetail());
        marketAddress.setAreaCode(wxAddressSaveBo.getAreaCode());
        marketAddress.setTel(wxAddressSaveBo.getTel());
        if ((wxAddressSaveBo.getIsDefault()).equals(false)) {
            marketAddress.setIsDefault(false);
        }
        if (wxAddressSaveBo.getIsDefault().equals(true)) {
            marketAddress.setIsDefault(true);
        }
        marketAddress.setAddTime(date);
        marketAddress.setUpdateTime(date);
        marketAddressMapper.insertSelective(marketAddress);
    }

    @Override
    public void updateAddressInfoByBo(WxAddressSaveBo wxAddressSaveBo) {
        Date date = new Date();
        MarketAddress marketAddress = new MarketAddress();
        //marketAddress.setId(wxAddressSaveBo.getId());
        // todo: 根据数据表字段顺序确定插入字段顺序
        marketAddress.setId(wxAddressSaveBo.getId());
        marketAddress.setName(wxAddressSaveBo.getName());
        marketAddress.setProvince(wxAddressSaveBo.getProvince());
        marketAddress.setCity(wxAddressSaveBo.getCity());
        marketAddress.setCounty(wxAddressSaveBo.getCounty());
        marketAddress.setAddressDetail(wxAddressSaveBo.getAddressDetail());
        marketAddress.setAreaCode(wxAddressSaveBo.getAreaCode());
        marketAddress.setTel(wxAddressSaveBo.getTel());
        if (wxAddressSaveBo.getIsDefault().equals(false)) {
            marketAddress.setIsDefault(false);
        }
        //marketAddress.setIsDefault(true);
        if (wxAddressSaveBo.getIsDefault().equals(true)) {
            marketAddress.setIsDefault(true);
        }
        marketAddress.setUpdateTime(date);
        marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
    }

    @Override
    public Integer queryDataInWxAddressSaveVo(WxAddressSaveBo wxAddressSaveBo, Integer currentUserId) {
        // todo: add、update共用一个save方法，两者的区别
        //  在于请求参数中携带的isDefault字段类型是Integer还是Boolean
        if (wxAddressSaveBo.getIsDefault().equals(0) || wxAddressSaveBo.getIsDefault().equals(1)) {
            //List<MarketAddress> marketAddressList = marketAddressMapper.selectByExample(null);
            List<MarketAddress> marketAddressList = marketAddressMapper.queryAddressListByUserId(currentUserId);
            // todo: 走到这里，说明执行的是add，vo中的data是数据库中的数据总数
            return marketAddressList.size();
        }
        // todo: 走到这里，说明执行的是update，vo中的data是
        //  旧数据的id，不是更新后的数据的id
        return wxAddressSaveBo.getId();
    }

    @Override
    public void deleteAddressInfoById(Integer id) {
        MarketAddress marketAddress = new MarketAddress();
        marketAddress.setId(id);
        marketAddress.setDeleted(true);
        marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
    }

    @Override
    public MarketAddress queryAddressByisDefaultAndDeletedAndUserId(Integer isDefault, boolean deleted, Integer currentUserId) {
        return marketAddressMapper.queryAddressByisDefaultAndDeletedAndUserId(isDefault, deleted, currentUserId);
    }

    @Override
    public void updateCurrentAddressInfoByIdAndisDefault(Integer id, boolean isDefault) {
        marketAddressMapper.updateCurrentAddressInfoByIdAndisDefault(id, isDefault);
    }

}
